package com.frog.service.friend.service.api;


import com.frog.service.friend.api.RequestMsgChatApi;
import com.frog.service.friend.enums.RequestChatOperateEnum;
import com.frog.service.friend.model.domain.RequestMsgStatusDO;
import com.frog.service.friend.service.msgchat.RequestMsgChatService;
import com.frog.service.friend.utils.FriendRelationshipUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class RequestMsgChatApiImpl implements RequestMsgChatApi {


    @Autowired
    private RequestMsgChatService requestMsgChatService;


    /**
     * 创建真假聊天邀请,用户和用户之间只能邀请一次
     */
    @Override
    public boolean create(String fromUserId, String toUserId) {
        if (StringUtils.isBlank(fromUserId) || StringUtils.isBlank(toUserId)) return false;
        // 若查询到结果集,说明两个用户已经邀请过了
        Long id = FriendRelationshipUtil.processRelationId(Integer.parseInt(fromUserId), Integer.parseInt(toUserId));
        RequestMsgStatusDO r = requestMsgChatService.selectRequestMsgChat(String.valueOf(id));
        if (r != null) return true;
        // 用户和用户之间只能邀请一次
        r = RequestMsgStatusDO.builder().id(id).fromUserId(Integer.parseInt(fromUserId)).toUserId(Integer.parseInt(toUserId)).build();
        return requestMsgChatService.createRequestMsgChat(r);
    }


    /**
     * 受邀人同意或拒绝发起人真假聊天邀请,只能回应一次
     */
    @Override
    public boolean operate(String userId, String friendUserId, RequestChatOperateEnum operateEnum) {
        if (StringUtils.isBlank(userId) || StringUtils.isBlank(friendUserId) || operateEnum == null) return false;
        // 查询邀请记录
        Long id = FriendRelationshipUtil.processRelationId(Integer.parseInt(userId), Integer.parseInt(friendUserId));
        RequestMsgStatusDO r = requestMsgChatService.selectRequestMsgChat(String.valueOf(id));
        // 验证邀请关系
        if (r == null || r.getFromUserId() == null || !String.valueOf(r.getFromUserId()).equals(friendUserId)) {
            log.error("userId=={} friendUserId=={} 没有创建过真假聊天,或者发起人不匹配", userId, friendUserId);
            return false;
        }
        if ("1".equals(r.getResponseStatus())) {
            log.error("userId=={} friendUserId=={} 已经处理过真假聊天请求", userId, friendUserId);
            return true;
        }
        // 同意/拒绝发起人真假聊天邀请
        return requestMsgChatService.operateRequestMsgChat(r, operateEnum);
    }


    /**
     * 查询真假聊天邀请
     */
    @Override
    public String select(String userId, String friendUserId) {
        if (StringUtils.isBlank(userId) || StringUtils.isBlank(friendUserId)) return null;
        // 查询邀请记录
        Long id = FriendRelationshipUtil.processRelationId(Integer.parseInt(userId), Integer.parseInt(friendUserId));
        RequestMsgStatusDO r = requestMsgChatService.selectRequestMsgChat(String.valueOf(id));
        // 邀请记录不能为空
        if (r == null) return null;
        // userId是发起人返回同意邀请,否则frienduserId为发起人operate状态
        return userId.equals(String.valueOf(r.getFromUserId())) ? "2" : r.getChatStatus();
    }


}